package com.crape.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.common.constants.MessageCode;
import com.crape.common.enums.ArticleStatusType;
import com.crape.common.model.Article;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.topic.bo.IArticleBO;
import com.crape.topic.bo.IArticleIndexBO;

@Controller
public class AdminArticleController {
	
	private IArticleIndexBO 	articleIndexBOImpl;
    private IArticleBO 		articleBOImpl;

    @Autowired
    public void setArticleIndexBOImpl(
			IArticleIndexBO articleIndexBOImpl) {
		this.articleIndexBOImpl = articleIndexBOImpl;
	}

	@Autowired
    public void setArticleBOImpl(IArticleBO articleBOImpl) {
        this.articleBOImpl = articleBOImpl;
    }

    @RequestMapping("/admin/topic/articleList.jsp")
    public String articleList(ModelMap modelMap, Long accountid, Long articleid, Integer indexType, Integer status, Integer pageNo) {
    	modelMap.put("accountid", accountid);
        modelMap.put("articleid", articleid);
    	modelMap.put("indexType", indexType);
        modelMap.put("status", status);
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        Integer pageSize = 20;
        Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
        Long counts = articleIndexBOImpl.countIndexArticles(accountid, articleid, indexType, status);
        modelMap.put("pageNo", pageNo);
        modelMap.put("counts", counts);
        if (counts == null || counts == 0) {
            modelMap.put("pageNum", 0);
            return "/admin/topic/listArticle.html";
        }
        modelMap.put("pageNum", PageUtils.getPagedNum(counts, pageSize));
        List<Article> articleList = articleIndexBOImpl.getIndexArticles(accountid, articleid, indexType, status, startIndex, pageSize);
        modelMap.put("articleList", articleList);
        return "/admin/topic/listArticle.html";
    }

    @RequestMapping("/admin/topic/recommendArticle.jsp")
    public String recommendArticle(ModelMap modelMap, Long articleid, Integer indexType, Integer status, Integer pageNo) {
    	Article article = articleBOImpl.getArticleId(articleid);
    	if(article==null){
    		modelMap.put(MessageCode.key, MessageCode.TOPIC_ARTICLE_IS_NULL);
    		return this.articleList(modelMap, null, null, null, null, pageNo);
    	}
    	if(article.getStatus()!=ArticleStatusType.published.getCode()){
    		modelMap.put(MessageCode.key, MessageCode.TOPIC_ARTICLE_UNPUBLISHED);
    		return this.articleList(modelMap, null, articleid, null, null, pageNo);
    	}
    	Long accountid = ThreadObjManager.getLoginUser().getAccountid();
    	articleIndexBOImpl.recommendArticle(accountid, articleid, indexType);
        return this.articleList(modelMap, null, null, null, status, pageNo);
    }
    
    @RequestMapping("/admin/topic/updateArticleStatus.jsp")
    public String updateStatus(ModelMap modelMap, Long articleid, Integer indexType, Integer status, Integer pageNo) {
    	Article article = articleBOImpl.getArticleId(articleid);
    	Long accountid = null;
    	if(article!=null){
    		accountid = article.getAccountid();
    		articleBOImpl.updateArticleStatus(accountid, articleid, status);
    	}
        return this.articleList(modelMap, accountid, articleid, indexType, status, pageNo);
    }

}
